package com.CMS.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.CMS.client.MultimediaService;
import com.CMS.server.dao.GaleryDao;
import com.CMS.server.dao.ImagesDao;
import com.CMS.server.dao.PageDao;
import com.CMS.server.domain.Galery;
import com.CMS.server.domain.Images;
import com.CMS.server.domain.Page;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class RpcMultimedia extends RemoteServiceServlet implements MultimediaService
{
	private static final long serialVersionUID = 8656566607925108962L;
	
	@Override
	public void setGallery(long idPage, String nameGalery) {		
		
		Galery gal = new Galery();
  		gal.setNameGalery(nameGalery);
  		gal.setPageAsociated(idPage);
  		
  		GaleryDao gDao = new GaleryDao();
  		gDao.createEntity(gal);
  		
  		ImagesDao imgDao = new ImagesDao(); 
  		
  		PageDao pageDao = new PageDao();
  		Page p = pageDao.getEntity(idPage);
  		
  		String root = getServletContext().getRealPath("/");
 	   
 	   File srcDir = new File(root + "/tempsUploads");
 	   File dstDir = new File(root + "/uploads");
 		     
 	    if (srcDir.isDirectory())
 	    {
              if (!dstDir.exists())
              {
                    dstDir.mkdir();
              }
              
              String[] children = srcDir.list();
              
              for(int i=0; i < children.length; i++)
              {	              	 
                try {
             		InputStream in = new FileInputStream(srcDir + "/" +  children[i]);
             		OutputStream out = new FileOutputStream(dstDir + "/" + children[i]);
             		 
             		byte[] buf = new byte[1024];
             	    int len;
             		 
             		while ((len = in.read(buf)) > 0)
             		{             			
             			out.write(buf, 0, len);             		   
             		}
             		Images img = new Images();
         			img.setName(children[i]);
         			img.setPage(p);
         			img.setGalery(gal);
         			imgDao.createEntity(img);
         			
             		in.close();
             		out.close();             		 
             		  } catch (IOException ioe){
             		    ioe.printStackTrace();
             		  }
              }               
              //borro archivos temporales             
              File[] ficheros = srcDir.listFiles();              
              for (int x=0;x<ficheros.length;x++)
              {
             	 ficheros[x].delete(); 
              }
              srcDir.delete();
         }
	}
	
	public boolean isSetImageGallery()
	{
		String root = getServletContext().getRealPath("/");
	 	   
	 	File srcDir = new File(root + "/tempsUploads");
	 	   
	 	if (srcDir.isDirectory())
	 	{
	 	  String[] children = srcDir.list();   
		  
	 	  return children.length > 0;
		
	 	}  
		return false;
		
	}
	
	@Override
	public List<Galery> fetch() {
		GaleryDao dao = new GaleryDao();
		return dao.getAll();
	}
	
	public List<Images> fetchImage(long idGalery) {
		
		ImagesDao dao = new ImagesDao();
		return dao.getAllImages(idGalery);
		
	}
	
	@Override
	public String validateGalery(String name)
	{
		
		GaleryDao dao = new GaleryDao();
		
		if(dao.isExistName(name))
		   return "El nombre de la galeria que quiere agregar ya existe";
		return "";
	}
	
	@Override
	public String validatePageGalery(long idPage)
	{
		
		GaleryDao dao = new GaleryDao();
		
		if(dao.asociatedPageGallery(idPage))
		   return "La pagina que ha seleccionado ya posee una galeria asociada";
		
		return "";
	}
	
	
	@Override
	public String getGaleryName(long id)
	{
		GaleryDao dao = new GaleryDao();
		
	    return dao.getEntity(id).getName();

	}
	
	public void updateNameGallery(String name,long idGalery)
	{
	  GaleryDao dao = new GaleryDao();	
	  Galery g = dao.getEntity(idGalery);
	  
	  g.setNameGalery(name);
	  g.setModDate(new Date());
	  dao.updateEntity(g);
		
	}
	
	public void updateStateImage(int state,long id)
	{
		ImagesDao dao = new ImagesDao();
		
		Images i = dao.getEntity(id);
		
		if(state == 0)
		   i.setVisible(1);
		else
		   i.setVisible(0);
			
		i.setModDate(new Date());
		dao.updateEntity(i);
	}
	
	@Override
	public void updateImageGallery(long idGalery) {		
		
		ImagesDao imgDao = new ImagesDao(); 
  		
		GaleryDao galDao = new GaleryDao();
  		
  		Galery g = galDao.getEntity(idGalery);
		
		PageDao pageDao = new PageDao();
  		Page p = pageDao.getEntity(g.getPage());
  		
  		String root = getServletContext().getRealPath("/");
 	   
 	   File srcDir = new File(root + "/tempsUploads");
 	   File dstDir = new File(root + "/uploads");
 		     
 	    if (srcDir.isDirectory())
 	    {
              if (!dstDir.exists())
              {
                    dstDir.mkdir();
              }
              
              String[] children = srcDir.list();
              
              for(int i=0; i < children.length; i++)
              {	              	 
                try {
             		InputStream in = new FileInputStream(srcDir + "/" +  children[i]);
             		OutputStream out = new FileOutputStream(dstDir + "/" + children[i]);
             		 
             		byte[] buf = new byte[1024];
             	    int len;
             		 
             		while ((len = in.read(buf)) > 0)
             		{             			
             			out.write(buf, 0, len);             		   
             		}
             		Images img = new Images();
         			img.setName(children[i]);
         			img.setPage(p);
         			img.setGalery(g);
         			imgDao.createEntity(img);
         			
             		in.close();
             		out.close();             		 
             		  } catch (IOException ioe){
             		    ioe.printStackTrace();
             		  }
              }               
              //borro archivos temporales             
              File[] ficheros = srcDir.listFiles();              
              for (int x=0;x<ficheros.length;x++)
              {
             	 ficheros[x].delete(); 
              }
              srcDir.delete();
         }
	}
}   
